package com.example.service;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * 内容提取服务
 * 职责：从HTML文档中提取文章内容
 * 功能：
 * - 判断页面是否为文章页
 * - 提取文章标题
 * - 提取文章正文
 * - 记录选择器匹配情况（调试模式）
 */
@Component
public class ContentExtractor {
    
    private final LogService logService;
    private final boolean debugMode;
    
    @Autowired
    public ContentExtractor(LogService logService, 
                          @Value("${crawler.debug:true}") boolean debugMode) {
        this.logService = logService;
        this.debugMode = debugMode;
    }
    
    /**
     * 判断页面是否为文章页
     */
    public boolean isArticlePage(Document doc) {
        // 检查页面是否包含文章特征
        String[] articleSelectors = {
            "article", 
            ".article", 
            ".post", 
            ".content",
            ".article-content",
            ".post-content",
            ".entry-content"
        };

        boolean isArticle = false;
        for (String selector : articleSelectors) {
            Elements elements = doc.select(selector);
            if (!elements.isEmpty()) {
                isArticle = true;
                if (debugMode) {
                    logService.logSelector(selector, elements.first().text(), doc.location(), true);
                }
            } else if (debugMode) {
                logService.logSelector(selector, "", doc.location(), false);
            }
        }

        if (debugMode) {
            String message = String.format("页面 %s 被判定为%s文章页面", 
                doc.location(), isArticle ? "" : "非");
            logService.logToFile("crawler.log", message);
        }

        return isArticle;
    }

    /**
     * 提取文章标题
     */
    public String extractTitle(Document doc, String url) {
        // 尝试不同的选择器
        String[] selectors = {
            "h1", 
            ".article-title", 
            ".post-title", 
            ".title", 
            "#title",
            ".entry-title",
            ".article-header h1",
            ".post-header h1",
            ".article-heading",
            ".post-heading"
        };

        for (String selector : selectors) {
            Elements elements = doc.select(selector);
            if (!elements.isEmpty()) {
                String content = elements.first().text();
                if (debugMode) {
                    logService.logSelector(selector, content, url, true);
                }
                return content;
            } else if (debugMode) {
                logService.logSelector(selector, "", url, false);
            }
        }
        return "";
    }

    /**
     * 提取文章正文
     */
    public String extractContent(Document doc, String url) {
        // 尝试不同的选择器
        String[] selectors = {
            "article", 
            ".article-content", 
            ".post-content", 
            ".content", 
            "#content",
            ".article-body",
            ".article-text",
            ".entry-content",
            ".post-body",
            ".article-main"
        };

        for (String selector : selectors) {
            Elements elements = doc.select(selector);
            if (!elements.isEmpty()) {
                String content = elements.first().text();
                if (debugMode) {
                    logService.logSelector(selector, content, url, true);
                }
                return content;
            } else if (debugMode) {
                logService.logSelector(selector, "", url, false);
            }
        }
        return "";
    }
} 